package com.lsh.util;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * Base4转File
 */
public class Base64toFileUtil {

    public final static String HeadPortraitName = "hp_name.jpg"; // 用户头像名

    public static void Base64toFile(String dirname, String filename, String base64file){
        base64file = base64file.substring(23); // 去掉base64编码的头部
        byte[] base64Byte = Base64.getDecoder().decode(base64file);
        File file = new File(dirname);
        if(!file.exists()) file.mkdirs(); // 判断目录是否存在
        dirname = dirname + filename; //与文件名拼接完整路径
        try {
            FileOutputStream fos = new FileOutputStream(dirname);
            fos.write(base64Byte);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
